package com.carlinfo.demo.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IADynastyDao;
import com.carlinfo.demo.pojo.ADynasty;


/**
 * 朝代的Dao实现类
 * 
 * 
 */
public class ADynastyDaoImpl extends BaseDaoImpl<ADynasty> implements IADynastyDao
{
	/**
	 *将数据库里面的记录变成POJO 
	 * @param rs
	 * @return
	 * 
	 */
	protected ADynasty converFromDb_Source(ResultSet rs) throws SQLException
	{
		/* 查询  数据 */
		int id = rs.getInt("id");
		String name = rs.getString("name");
		String capital = rs.getString("capital");
		int age = rs.getInt("age");
		String context = rs.getString("context");
		byte status = rs.getByte("status");
		Date st = rs.getTimestamp("st");
		Date createTime = rs.getTimestamp("createTime");
		Date updateTime = rs.getTimestamp("updateTime");
		Date pubTime = rs.getTimestamp("pubTime");
		ADynasty dynasty = new ADynasty();
		dynasty.setId(id);
		dynasty.setName(name);
		dynasty.setCapital(capital);
		dynasty.setAge(age);
		dynasty.setContext(context);
		dynasty.setStatus(status);
		dynasty.setSt(st);
		dynasty.setCreateTime(createTime);
		dynasty.setUpdateTime(updateTime);
		dynasty.setPubTime(pubTime);
		return dynasty;

	}

	@Override
	public int saveOneDao(ADynasty dynasty)
	{
		String sql = "insert into a_dynasty values(null,?,?,?,?,?,?,?,?,?)";
		List<Object> paramsList = new ArrayList<Object>();
		/* 为占位符赋值 */
		paramsList.add(dynasty.getName());
		paramsList.add(dynasty.getCapital());
		paramsList.add(dynasty.getAge());
		paramsList.add(dynasty.getContext());
		paramsList.add(dynasty.getStatus());
		paramsList.add(dynasty.getSt());
		paramsList.add(dynasty.getCreateTime());
		paramsList.add(dynasty.getUpdateTime());
		paramsList.add(dynasty.getPubTime());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int res = this.executeJDBC(sql, paramsList, resultMap);
		dynasty.setId(Integer.valueOf(resultMap.get("id") + ""));
		return res;
	}

	@Override
	public int updateOneDao(ADynasty dynasty)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "update a_dynasty set name = ? ,capital = ? ," + "age = ? , context = ? , status = ? , st = ? ,"
				+ "createTime = ? , updateTime = ? , pubTime = ? " + " where id = ? ";

		/* 为占位符赋值 */
		paramsList.add(dynasty.getName());
		paramsList.add(dynasty.getCapital());
		paramsList.add(dynasty.getAge());
		paramsList.add(dynasty.getContext());
		paramsList.add(dynasty.getStatus());
		paramsList.add(dynasty.getSt());
		paramsList.add(dynasty.getCreateTime());
		paramsList.add(dynasty.getUpdateTime());
		paramsList.add(dynasty.getPubTime());
		paramsList.add(dynasty.getId());

		return this.executeJDBC(sql, paramsList);
	}

	/**
	 * 根据id删除 delete from a_dynasty where id = ? delect from a_dynasty where name =
	 * ?
	 */
	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "delete from a_dynasty ";
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql = sql + " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (condMap.get("name") != null && !"".equalsIgnoreCase(condMap.get("name") + ""))
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		/*
		 * JDBC的更新 先写JDBC的架构
		 */
		/* 加载驱动+获取链接 */
	/*	DbUtil dbUtil = DbUtil.getInstance();
		Connection conn = dbUtil.getConn();
		PreparedStatement pst = null;
		try
		{
			this.logger.info("sql:{},paramList:{}", sql, paramsList);
			pst = conn.prepareStatement(sql);
			int count = 1;
			 为占位符赋值 
			for (Iterator iterator = paramsList.iterator(); iterator.hasNext();)
			{
				Object val = (Object) iterator.next();
				pst.setObject(count, val);
				count++;
			}
			int res = pst.executeUpdate();
			return res;
		} catch (SQLException e)
		{
			// e.printStackTrace();
			this.logger.error("朝代删除失败了:", e);
		} finally
		{
			dbUtil.closeAll(conn, pst, null);
		}*/
		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public ADynasty selectOneDao(Map<String, Object> condMap)
	{
		/* 准备一个容器,这里面放的就是参数 */
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_dynasty ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + " where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		/*
		 * 加载驱动+获取链接 DbUtil dbUtil = DbUtil.getInstance(); Connection conn =
		 * dbUtil.getConn(); PreparedStatement pst = null; ResultSet rs = null; try {
		 * this.logger.info("sql:{},paramList:{}", sql, paramsList); pst =
		 * conn.prepareStatement(sql); int count = 1; for (Iterator iterator =
		 * paramsList.iterator(); iterator.hasNext();) { Object val = (Object)
		 * iterator.next(); pst.setObject(count, val); count++; } rs =
		 * pst.executeQuery(); if (rs.next()) { ADynasty dynasty = converFromDb(rs);
		 * return dynasty; } } catch (SQLException e) { e.printStackTrace(); } finally {
		 * dbUtil.closeAll(conn, pst, rs); }
		 */
		/* 调用converFromDb先初始化pojo */
		this.pojoObj = new ADynasty();
		return this.findOneJDBC(sql, paramsList);
	}

	/**
	 * 分页: ~想办法把SQL语句加上limit
	 */
	@Override
	public List<ADynasty> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		List<ADynasty> dynastyList = new ArrayList<ADynasty>();
		/* sql语句 */
		String sql = "select * from a_dynasty where 1 = 1";
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and (name like ? or capital like ? or context like ?)";
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			paramsList.add(condMap.get("status"));
		}
		/*
		 * 时间志勇判断是不是Date类型就可以 null intanceof Date:返回值是true还是false
		 */
		if (condMap.get("stDate") instanceof Date && condMap.get("esDate") instanceof Date)
		{
			sql += " and ( pubTime >= ? and pubTime < ? )";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("edDate"));
		}
		/* 排序 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:降序 */
			sql += " order by id asc, name desc";
		} else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		/*
		 * 写?的时候想想如何赋值 从PageInfo中获取---(传参)
		 * 
		 * 
		 * JDBC查询多条朝代 DbUtil dbUtil = DbUtil.getInstance(); Connection conn =
		 * dbUtil.getConn(); PreparedStatement pst = null; ResultSet rs = null; try {
		 * int count = 1; if (pageInfoUtil != null) { String fromIndex = "from"; String
		 * countSQL = "select count(*) " + sql.substring(sql.indexOf(fromIndex),
		 * sql.length()); this.logger.info("countSQL:{},参数:{}", countSQL, paramsList);
		 * 获取sql语句对象 pst = conn.prepareStatement(countSQL); 为占位符赋值 count = 1; for
		 * (Iterator iterator = paramsList.iterator(); iterator.hasNext();) { Object val
		 * = (Object) iterator.next(); pst.setObject(count, val); count++;
		 * 
		 * } 执行sql语句 rs = pst.executeQuery(); if (rs.next()) { int totalRecord =
		 * rs.getInt(1); pageInfoUtil.setTotalRecord(totalRecord); } 关闭 rs.close();
		 * pst.close(); sql = sql + " limit ? , ? ";
		 * paramsList.add(pageInfoUtil.getCurrentRecord());
		 * paramsList.add(pageInfoUtil.getPageSize()); }
		 * this.logger.info("sql:{},参数:{}", sql, paramsList); pst =
		 * conn.prepareStatement(sql); count = 1; for (Iterator iterator =
		 * paramsList.iterator(); iterator.hasNext();) { Object val = (Object)
		 * iterator.next(); pst.setObject(count, val); count++;
		 * 
		 * } rs = pst.executeQuery();
		 * 
		 * 如果是查询一条,用if 如果是查询多条用while
		 * 
		 * while (rs.next()) {
		 * 
		 * 查询数据 int id = rs.getInt("id"); String name = rs.getString("name"); String
		 * capital = rs.getString("capital"); int age = rs.getInt("age"); Date st =
		 * rs.getTimestamp("st"); String context = rs.getString("context"); byte status
		 * = rs.getByte("status"); Date createTime = rs.getTimestamp("createTime"); Date
		 * updateTime = rs.getTimestamp("updateTime"); Date pubTime =
		 * rs.getTimestamp("pubTime");
		 * 
		 * 不能打印,要返回 new出来的属性全部是空的 经数据库取出来的值,一一赋值给属性
		 * 
		 * 
		 * ADynasty dynasty = new ADynasty(); dynasty.setId(id); dynasty.setName(name);
		 * dynasty.setCapital(capital); dynasty.setAge(age);
		 * dynasty.setContext(context); dynasty.setSt(st); dynasty.setStatus(status); ;
		 * dynasty.setUpdateTime(updateTime); dynasty.setCreateTime(createTime);
		 * dynasty.setPubTime(pubTime); 放到集合中 dynastyList.add(dynasty);
		 * 
		 * ADynasty dynasty = this.converFromDb(rs); dynastyList.add(dynasty); } 将集合返回
		 * return dynastyList; } catch (SQLException e) { logger.error("朝代操作查询多条记录失败",
		 * e); } finally { dbUtil.closeAll(conn, pst, rs); } 比返回null强.不会报空指针
		 */
		/* 调用converFromDb先初始化pojo */
		this.pojoObj = new ADynasty();
		if (pageInfoUtil != null)
		{
			/* 分页 */
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		/* 不分页 */
		return this.findList(sql, paramsList);
	}

}
